/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.sd.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sd.common.util.UploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

/**
 * 拍照上传Controller
 *
 * @author linyh
 * @version 2020-07-09
 */
@Slf4j
@Controller
@RequestMapping(value = "${adminPath}/sd/collectPicture")
public class CollectPictureController extends BaseController {

    /**
     * 跳转拍照页面
     */
    @RequestMapping(value = "form")
    public String form() {
        return "modules/sd/collectPicture";
    }

    /**
     * 上传照片
     */
    @PostMapping(value = "uploadFile")
    @ResponseBody
    public String uploadFile(MultipartFile file) throws IOException {

        if (Objects.isNull(file)) {
            return renderResult(Global.FALSE, text("请拍照后再提交！"));
        }

        String filePath = UploadUtil.uploadPicture(file);

        if (StringUtils.isNotBlank(filePath)) {
            return renderResult(Global.TRUE, text("提交成功！"), filePath);
        } else {
            return renderResult(Global.FALSE, text("提交失败！"));
        }
    }

    /**
     * 显示图片
     *
     * @param src   传过来的路径需要是/ ex:D:/CustomUploadPicture/1595320430646.png
     * @param model 页面已判断是否获取到图片
     * @return
     */
    @RequestMapping(value = "showImg")
    public String showImg(String src, Model model) {


        model.addAttribute("imgCode", src);
        return "modules/sd/showImg";
    }

    /**
     * 跳转上传图片页面
     *
     * @return
     */
    @RequiresPermissions("sd:docDeliverTake:edit")
    @RequestMapping(value = "upload")
    public String upload() {
        return "modules/sd/uploadPicture";
    }

}